/**
 * Created by zzhonghua on 2017/5/31.
 */

$(document).ready(function(){
//  var ajaxData,shareList,feedId,userId;
//  init();
//
////初始化页面
//function init(){
//  $.ajax({
//      url: '/api/v1/person/details',
//      type: "post",
//      async: true,
//      success: function (data) {
//          ajaxData = data;
//          initPage(data);
//          console.log(data.result);
//      },
//      error: function (msg) {
//          //alert('请求失败');
//          console.log(msg)
//      }
//  });
//}
//点赞or取消赞
    $('.dianzan').click(isLike);

//创建评论
    $('.comment').click(commentEdit);

//查看帖子详情
    /*$('#share .container').click(shareDetail);

     //帖子详情
     function shareDetail(shareList){
     var index = $(this).index();
     feedId = shareList[index].id;
     var data = {
     "feedId": feedId,
     "userId": userId
     };
     $.ajax({
     type: "get",
     url: '/api/v1/feedService/get/one',
     data:data,
     success: function (data) {

     },
     error: function (msg) {
     alert('请求失败');
     console.log(msg)
     }
     });

     }*/

//点赞or取消赞
    function isLike(){
        var likeCount = $(this).children('.dianzan-count').text();
        var liked=$(this).children('.liked').val();
        var fid=$(this).children(".fid").val();
        var userId=localStorage.getItem("userId");
       // alert(userId);
        var obj=$(this)
        var data = {
            "customerId":userId,
            "id": fid,
        };
        if (liked==0){
            $.ajax({
                type: "get",
                url: '/api/v1/feedService/like/create',
                data:data,
                success: function (data) {
                    obj.children('img').attr('src','/static/h5/images/dianzan.png');
                   // alert("点赞成功！");
                    //点赞成功之后把值变成1
                    obj.children(".liked").val(1);
                    obj.children(".dianzan-count").text(" ");
                    likeCount=parseInt(likeCount)+parseInt(1);
                    obj.children('.feelicount').val(likeCount)
                    obj.children(".dianzan-count").text(likeCount);
                },
                error: function (msg) {
                    alert('请求失败');
                    console.log(msg)
                }
            });
        }
        else{
            $.ajax({
                type: "post",
                url: '/api/v1/feedService/like/delete',
                data:data,
                success: function (data) {
                    obj.children('img').attr('src','/static/h5/images/dianzan1.png');
                 //   alert("取消成功！");
                    obj.children(".liked").val(0);
                    obj.children(".dianzan-count").text(" ");
                    likeCount=likeCount-1;
                    obj.children('.feelicount').val(likeCount)
                    obj.children(".dianzan-count").text(likeCount);
                },
                error: function (msg) {
                    alert('请求失败');
                    console.log(msg)
                }
            });
        }
    }


//新增评论
    function commentEdit() {

        var index = $(this).parents("li").index()//点击第几条数据的检索值
        var obj=$(this)
        var disCount = $(this).children('.pinglun-count').text();
        var liked=$(this).children('.liked').val();
        var userId=localStorage.getItem("userId");
        $(".cmt_cont").val("")//初始化

        $(".cmt_btm").animate({
            bottom: "0"
        }, 500)

        // 发表评论
        $("#cmt_issue").on("click",function () {
            var toArea = $("#share").children("ul").children("li").eq(index).find(".cmt_area")//添加数据位置
            console.log(toArea)
            var feedId = $(".fid").val()//评论
            var content = $(".cmt_cont").val()//内容
            var data ={
                "userId":userId,
                "content":content,
                "id":feedId
            }
            if(content.trim().length==0 || content==null){
                alert("请输入内容")
                return false;
            }else{
                $.ajax({
                    type:"post",
                    url:"/api/v1/feedService/comment/create",
                    data:data,
                    success:function(data){
                        name=data.data.customer.name
                        obj.children(".pinglun-count").text(" ");
                        disCount=parseInt(disCount)+parseInt(1)
                        obj.children(".pinglun-count").text(disCount);
                        toArea.prepend('<p class="issue_cont"><span class="issue_id">'+name+'</span><span>：</span><span>'+data.data.content+'</span></p>')
                        $(".cmt_area").animate({
                            bottom: "-40px"
                        },500)
                    },
                    error:function(err){
                        //alert("issue failed")
                    }

                })
            }
        })

        return false
    }
//阻止事件冒泡
    $(".cmt_btm").on("click",function(){
        return false
    })

    $("body").on("click",function(){
        $(".cmt_btm").animate({
            bottom:"-40px"
        },500)
    })
});

//历程
$(function(){
    $("#up_Img").on('click',function(){
        $(".popup").animate({
            bottom:"0"
        },1000)
    })

    $(".cancle").on("click",function(){
        $(".popup").animate({
            bottom:"-220px"
        },1000)

    })
    $(".issue_img").on("click",function(){
        $(".popup").hide()
        $(".process").hide()
        $(".frame_box").show()
        $("#iss").show()
        $(".popup").animate({
            bottom:"-220px"
        },1000)
    })

    $("#iss").on("click",function(){

        if(Img.src !=""){
            $(".popup").show()
            $(".process").show()
            $(".frame_box").hide()
            $("#iss").hide()
            rec_Img.src = Img.src
        }

    })

})
//发送分享
$(".share_btn").click(createShare);
//创建分享
function createShare(){
    $("#message_share").hide()
    $("#publish").show()

    $(".cancle_share").on("click",function(){
        $("#message_share").show()
        $("#publish").hide()
    })

    $(".issue_share button").on("click",function(){
        if($(".share_cont").val()==""){
            $(".issue_share button").attr({
                "disabled":false
            })
        }else{
            $("#share ul li").remove()
            $("#message_share").show()
            $("#publish").hide()

            customerId=localStorage.getItem("userId");
            var content = $(".share_cont").val()
            var data = {
                "userId":customerId,
                "tagId":3,//家族动态的id 固定
                "content":content,
            }
            $.ajax({
                type: "post",
                url: '/api/v1/feedService/create',//接口地址？
                data: data,
                success: function (data) {
                    console.log('请求成功');
                   /* $("#share ul").prepend('<li class="container"><div class="row"><div class="col-xs-6 share-time">'
                        +'<div><span class="share-time-day">27</span><span class="share-time-mouth">3</span>月</div></div>'
                        +'<div class="col-xs-6 share-content"><img src="${ctx}/static/h5/images/share-img.png" class="share-img" alt=""/>'
                        +'<span class="share-text">'+issFont+'</span></div></div><div class="row share-method"><div class="col-xs-6 dianzan">'
                        +' <img src="${ctx}/static/h5/images/dianzan1.png" alt=""/><span class="dianzan-count">16</span></div><div class="col-xs-6 comment">'
                        +'<img src="${ctx}/static/h5/images/pinglun.png" alt=""/><span class="pinglun-count">6</span></div></div></li>')*/
                    window.location.href="/h5/index/home?userId="+customerId;
                },
                error: function (msg) {
                    console.log('请求失败');
                    console.log(msg)
//	                    $("#share ul").prepend('<li class="container"><div class="row"><div class="col-xs-6 share-time">'
//                  +'<div><span class="share-time-day">27</span><span class="share-time-mouth">3</span>月</div></div>'
//                  +'<div class="col-xs-6 share-content"><img src="../../images/share-img.png" class="share-img" alt=""/>'
//                  +'<span class="share-text">'+data.issFont+'</span></div></div><div class="row share-method"><div class="col-xs-6 dianzan">'
//                  +' <img src="../../images/dianzan1.png" alt=""/><span class="dianzan-count">16</span></div><div class="col-xs-6 comment">'
//                  +'<img src="../../images/pinglun.png" alt=""/><span class="pinglun-count">6</span></div></div></li>')
                }
            });

        }
    })
}
//分享时获取地理位置
$("#locMess").on("click",function(){
    var pos = new BMap.Geolocation()
    pos.getCurrentPosition(function(suc){
        $("#locMess").html("")
        $("#locMess").html(suc.address.street)
    })
})
//
//删除评论
$(".issue_cont").on("click",function(){
    var userid=$(this).children(".userid").val();
    var userId=window.localStorage.getItem("userId");
    if(userid==userId){
    var obj = $(this)
    $(".del_cmt").animate({
        bottom:"0"
    },300)
    var feedId=$(this).children(".commentId").val();
    var userId=localStorage.getItem("userId");
    var count=$(this).parent(".cmt_area").siblings(".share-method").children(".comment").children(".pinglun-count");
    var text=count.text();
    $(".del").on("click",function(){
        $(".del_cmt").animate({
            bottom:"-75px"
        },300)
        // @parm feedid,userId,
        var data={
            "customerId":userId,
            "commentId":feedId
        }
        $.ajax({
            type:"post",
            url:"/api/v1/feedService/comment/delete",
            data:data,
            success:function(data){
                obj.remove();
                count.text(" ");
                count.text(parseInt(text)-parseInt(1));
            },
            error:function (data) {

            }
        });


    })
    $(".cancle").on("click",function () {
        $(".del_cmt").animate({
            bottom:"-75px"
        },300)

    })
    $("body").on("click",function () {
        $(".del_cmt").animate({
            bottom:"-75px"
        },300)
    })
    return false
    }
});